from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
from model.ModSuperviseRecord import SuperviseRecord, superviseRecordSchema
from model.ModWorkRecord import WorkRecord, workRecordSchema
from database import Session, engine
import datetime
from sqlalchemy.sql import text
from sqlalchemy import or_
import pendulum
import uuid


def get_supervise_detail(session, bureauid: str, meter_barcode: str, serial_number: str):
    # 根据条码及初始序列号查询督办记录详细信息
    supervise_record_detail = session.query(SuperviseRecord).filter(SuperviseRecord.bureauid == bureauid).filter(
        SuperviseRecord.meter_barcode == meter_barcode).filter(
        SuperviseRecord.serial_number == serial_number).order_by(SuperviseRecord.time_station.desc()).all()
    return JSONResponse(superviseRecordSchema.dump(supervise_record_detail, many=True).data)


def get_supervise_confirm_list(session, bureauid: str, flag_result: str):
    # 督办记录返回反馈2,3/未反馈列表0
    if flag_result == "0":
        record_list = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.work_department == "1").filter(WorkRecord.confirm_openid.isnot(None)).filter(
            WorkRecord.final_result_flag == "0").filter(
            WorkRecord.flag_result == flag_result).order_by(WorkRecord.opentime.desc()).all()
    else:
        record_list = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.final_result_flag == "0").filter(
            or_(WorkRecord.flag_result == "2", WorkRecord.flag_result == "3")).filter(
            WorkRecord.confirm_openid.isnot(None)).filter(
            WorkRecord.work_department == "1").order_by(WorkRecord.opentime.desc()).all()
    return JSONResponse(workRecordSchema.dump(record_list, many=True).data)


def update_result_flag(session, bureauid: str, serial_number: uuid, meter_barcode: str, flag_result: str, openid: str,
                       time_operation: datetime, name_operation: str, remark_operation: str,
                       photo_fingerprint_operation: str):
    # 督办核实页面更新是否解决等标志
    try:
        if flag_result == "1":
            wrecord = session.query(WorkRecord).filter(WorkRecord.serial_number == serial_number).filter(
                WorkRecord.bureauid == bureauid).filter(WorkRecord.meter_barcode == meter_barcode).one_or_none()
            wrecord.flag_result = flag_result
            wrecord.final_result_flag = flag_result

            srecord = session.query(SuperviseRecord).filter(SuperviseRecord.serial_number == str(serial_number)).filter(
                SuperviseRecord.bureauid == bureauid).filter(SuperviseRecord.meter_barcode == meter_barcode).order_by(
                SuperviseRecord.time_station.desc()).first()
            srecord.openid = openid
            srecord.flag_result = flag_result
            srecord.time_operation = time_operation
            srecord.name_operation = name_operation
            srecord.remark_operation = remark_operation
            srecord.photo_fingerprint_operation = photo_fingerprint_operation
        else:
            wrecord = session.query(WorkRecord).filter(WorkRecord.serial_number == serial_number).filter(
                WorkRecord.bureauid == bureauid).filter(WorkRecord.meter_barcode == meter_barcode).one_or_none()
            print(workRecordSchema.dump(wrecord, many=False).data)
            wrecord.flag_result = flag_result
            srecord = session.query(SuperviseRecord).filter(SuperviseRecord.serial_number == str(serial_number)).filter(
                SuperviseRecord.bureauid == bureauid).filter(SuperviseRecord.meter_barcode == meter_barcode).order_by(
                SuperviseRecord.time_station.desc()).first()
            srecord.openid = openid
            srecord.flag_result = flag_result
            srecord.time_operation = time_operation
            srecord.name_operation = name_operation
            srecord.remark_operation = remark_operation
            srecord.photo_fingerprint_operation = photo_fingerprint_operation
    except Exception as e:
        raise


def get_supervise_station_list(session, bureauid: str, stationid: str):
    # 督办供电所反馈页面列表
    record_list = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
        WorkRecord.final_result_flag == "0").filter(
        or_(WorkRecord.flag_result == "0", WorkRecord.flag_result == "4")).filter(
        WorkRecord.confirm_openid.isnot(None)).filter(
        WorkRecord.work_department == "1").filter(WorkRecord.stationid == stationid).order_by(
        WorkRecord.opentime.desc()).all()
    return JSONResponse(workRecordSchema.dump(record_list, many=True).data)


def get_supervise_station_detail(session, bureauid: str, meter_barcode: str, serial_number: uuid):
    # 根据条码及序列号查询记录详细信息
    meter_info = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
        WorkRecord.meter_barcode == meter_barcode).filter(WorkRecord.serial_number == serial_number).one_or_none()
    return JSONResponse(workRecordSchema.dump(meter_info, many=False).data)


def insert_supervise_record(session, record):
    # 新增供电所督办反馈工作记录
    try:
        session.add(record)
        wrecord = session.query(WorkRecord).filter(WorkRecord.serial_number == record.serial_number).filter(
            WorkRecord.bureauid == record.bureauid).filter(
            WorkRecord.meter_barcode == record.meter_barcode).one_or_none()
        wrecord.flag_result = record.flag_result
    except Exception as e:
        print(e)
        raise


# def update_serial_number_operation(session, bureauid: str, serial_number_operation: str, meter_barcode: str,
#                                  openid: str, time_operation: datetime, name_operation: str):
# 运维协助页面更新序列号
#    try:
#        srecord = session.query(SuperviseRecord).filter(SuperviseRecord.flag_result == "3").filter(
#            SuperviseRecord.bureauid == bureauid).filter(SuperviseRecord.meter_barcode == meter_barcode).order_by(
#            SuperviseRecord.time_station.desc()).first()
#        srecord.serial_number_operation = serial_number_operation
#        srecord.openid = openid
#        srecord.time_operation = time_operation
#       srecord.name_operation = name_operation
#    except Exception as e:
#       raise


def get_supervise_info(session, bureauid: str, meter_barcode: str):
    # 根据条码查询督办记录
    supervise_record_list = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
        WorkRecord.work_department == "1").filter(
        WorkRecord.confirm_openid.isnot(None)).filter(
        WorkRecord.meter_barcode == meter_barcode).order_by(WorkRecord.opentime.desc()).all()
    return JSONResponse(workRecordSchema.dump(supervise_record_list, many=True).data)


def get_supervise_confirm_count(session, bureauid: str):
    # 查询待运维组核实的数量(角标)
    if bureauid == "00":
        record_list = session.query(WorkRecord).filter(WorkRecord.final_result_flag == "0").filter(
            or_(WorkRecord.flag_result == "2", WorkRecord.flag_result == "3")).filter(
            WorkRecord.confirm_openid.isnot(None)).filter(
            WorkRecord.work_department == "1").order_by(WorkRecord.opentime.desc()).all()
        list_cou = workRecordSchema.dump(record_list, many=True).data
    else:
        record_list = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.final_result_flag == "0").filter(
            or_(WorkRecord.flag_result == "2", WorkRecord.flag_result == "3")).filter(
            WorkRecord.confirm_openid.isnot(None)).filter(
            WorkRecord.work_department == "1").order_by(WorkRecord.opentime.desc()).all()
        list_cou = workRecordSchema.dump(record_list, many=True).data
    return len(list_cou)


def get_supervise_confirm_station_count(session, bureauid: str, stationid: str):
    # 查询供电所督办记录的数量(角标)
    record_list = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
        WorkRecord.final_result_flag == "0").filter(
        or_(WorkRecord.flag_result == "0", WorkRecord.flag_result == "4")).filter(
        WorkRecord.confirm_openid.isnot(None)).filter(
        WorkRecord.work_department == "1").filter(WorkRecord.stationid == stationid).order_by(
        WorkRecord.opentime.desc()).all()
    list_cou = workRecordSchema.dump(record_list, many=True).data
    return len(list_cou)
